package com.jie.langlangmall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.common.api.CommonPage;
import com.jie.langlangmall.common.constant.DeleteStatus;
import com.jie.langlangmall.common.constant.StatusConstant;
import com.jie.langlangmall.mapper.PmsBrandMapper;
import com.jie.langlangmall.mapper.PmsProductMapper;
import com.jie.langlangmall.model.PmsBrand;
import com.jie.langlangmall.model.PmsProduct;
import com.jie.langlangmall.model.PmsProductExample;
import com.jie.langlangmall.portal.dao.HomeDao;
import com.jie.langlangmall.portal.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/26
 * @Email 3217998214@qq.com
 * @Description 品牌信息
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private HomeDao homeDao;
    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private PmsProductMapper productMapper;

    /**
     * 获取推荐品牌
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<PmsBrand> recommendList(Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return homeDao.getRecommendBrandList(offset, pageSize);
    }

    /**
     * 获取品牌详情
     *
     * @param brandId
     * @return
     */
    @Override
    public PmsBrand detail(Long brandId) {
        return brandMapper.selectByPrimaryKey(brandId);
    }

    /**
     * 获取该品牌下的商品
     *
     * @param brandId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public CommonPage<PmsProduct> productList(Long brandId, Integer pageNum, Integer pageSize) {
        // select * from pms_product where brand_id=? and publish_status=1 and deleteStatus=0
        PageHelper.startPage(pageNum, pageSize);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andDeleteStatusEqualTo(DeleteStatus.NO_DELETE.getCode())
                .andPublishStatusEqualTo(StatusConstant.ENABLE)
                .andBrandIdEqualTo(brandId);
        List<PmsProduct> productList = productMapper.selectByExample(example);
        return CommonPage.restPage(productList);
    }
}
